<?php
namespace org\kahikai\database\mysql;

use org\kahikai\database\AliasDao;
use org\kahikai\database\DevelopmentDao;
use org\kahikai\database\GeneDao;
use org\kahikai\database\GeneExpressionDao;
use org\kahikai\database\PaperDao;
use org\kahikai\database\PhylogenyDao;
use org\kahikai\database\SpeciesDao;
use org\kahikai\domain\Species;

require_once dirname(__FILE__) . '/../AliasDao.php';
require_once dirname(__FILE__) . '/../DevelopmentDao.php';
require_once dirname(__FILE__) . '/../GeneDao.php';
require_once dirname(__FILE__) . '/../GeneExpressionDao.php';
require_once dirname(__FILE__) . '/../PaperDao.php';
require_once dirname(__FILE__) . '/../PhylogenyDao.php';
require_once dirname(__FILE__) . '/../SpeciesDao.php';
require_once dirname(__FILE__) . '/KahikaiMysql.php';
require_once dirname(__FILE__) . '/../../domain/Species.php';

/**
 * 
 * @author ch4mp
 *
 */
class MysqlSpeciesDao implements SpeciesDao {
	private $aliasDao;
	private $developmentDao;
	private $geneDao;
	private $geneExpressionDao;
	private $paperDao;
	private $phylogenyDao;
	
	public function __construct(
			AliasDao $anAliasDao,
			PaperDao $aPaperDao,
			PhylogenyDao $aPhylogenyDao,
			GeneDao $aGeneDao,
			DevelopmentDao $aDevelopmentDao,
			GeneExpressionDao $aGeneExpressionDao) {
		$this->aliasDao = $anAliasDao;
		$this->developmentDao = $aDevelopmentDao;
		$this->geneDao = $aGeneDao;
		$this->geneExpressionDao = $aGeneExpressionDao;
		$this->paperDao = $aPaperDao;
		$this->phylogenyDao = $aPhylogenyDao;
	}
	
	/**
	 * 
	 * @return a map of all Species id by Species name
	 */
	public function getAllNames() {
		$species = array();
		
		$result = KahikaiMysql::query('SELECT id, name FROM SPECIES');
		while($record = $result->fetch_assoc()) {
			$species[$record['name']] = $record['id'];
		}
		$result->close();
		
		return $species;
	}
	
	/**
	 *
	 * @param string a Species name
	 * @return the Species with specified name
	 */
	public function getByName($name) {
		$species = array();
		
		$result = KahikaiMysql::query("SELECT id FROM SPECIES WHERE name='$name'");
		while($record = $result->fetch_assoc()) {
			$species[$record['name']] = $this->get($record['id'], $name);
		}
		$result->close();
		
		return $species;
	}
	
	/**
	 *
	 * @param int a Species id
	 * @return the Species with specified id or null
	 */
	public function getById($id) {
		$species = array();
		
		$result = KahikaiMysql::query("SELECT name FROM SPECIES WHERE id=$id");
		while($record = $result->fetch_assoc()) {
			$species[$record['name']] = $this->get($id, $record['name']);
		}
		$result->close();
		
		return $species;
	}
	
	private function get($id, $name) {
		$species = new Species($id, $name);
		$species->aliases = $this->aliasDao->getBySpeciesId($id);
		$species->development = $this->developmentDao->getBySpeciesId($id);
		$species->genesStepsExpression = $this->geneExpressionDao->getBySpeciesId($id);
		$species->papers = $this->paperDao->getBySpeciesId($id);
		$speciesPhylogenies = $this->phylogenyDao->getAllSpeciesLeaves();
		$species->phylogenies = $speciesPhylogenies[$id];
		
		return $species;
	}
}
?>